package net.thermetics.fx.auth;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class GaeUser implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static enum AppRole implements GrantedAuthority {
	    ADMIN (0),
	    NEW_USER (1),
	    USER (2),
	    STAFF (4);

	    private int bit;

	    AppRole(int bit) {
	        this.bit = bit;
	    }

	    public String getAuthority() {
	        return toString();
	    }

		public int getBit() {
			return bit;
		}
	}

	private String userId;
	private String email;
	private String nickname;
	private Set<AppRole> authorities;
	private boolean staff;
	private boolean enabled = true;
	private HashMap<String, String> prefs = new HashMap<String, String>();
	private String apiKey;
	
	public GaeUser(String userID, String nickname, String email, String apiKey) {
		setUserId(userID);
		setNickname(nickname);
		setEmail(email);
		setApiKey(apiKey);
	}
	
	public GaeUser(String userID, String nickname, String email, Set<AppRole> roles,
					boolean staff, boolean enabled, HashMap<String, String> prefs, String apiKey) {
		setUserId(userID);
		setNickname(nickname);
		setEmail(email);
		setAuthorities(roles);
		setStaff(staff);
		setEnabled(enabled);
		setPrefs(prefs);
		setApiKey(apiKey);
	}
	
	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	public String getUserId() {
		return userId;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getEmail() {
		return email;
	}
	
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	
	public String getNickname() {
		return nickname;
	}
	
	public void setAuthorities(Set<AppRole> authorities) {
		this.authorities = authorities;
	}
	
	public Set<AppRole> getAuthorities() {
		return authorities;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public boolean isEnabled() {
		return enabled;
	}
	
	public Key getKey() {
		return KeyFactory.createKey("GaeUser", getUserId());
	}

	public void setStaff(boolean staff) {
		this.staff = staff;
	}

	public boolean isStaff() {
		return staff;
	}
	
	public void setPrefs(HashMap<String, String> prefs) {
		this.prefs = prefs;
	}
	
	public HashMap<String, String> getPrefs() {
		return prefs;
	}
	
	public String getApiKey() {
		return apiKey;
	}
	
	public void setApiKey(String apiKey) {
		this.apiKey = apiKey;
	}

}